<?php

namespace App\Repositories\Policy\Eloquent;

use App\Repositories\Policy\Contracts\SnailInterface;
use App\Services\Common\Common;
use Curl\CurlService\CurlService;
use Curl\StrService\DateService;

class SnailRepository implements SnailInterface
{

    protected $key = '3d855cf250177172ff14ec1291f09280';
    protected $token = '031929ac99c89b1a78a6ed4a4af06568';
    protected $appid = '3910';
    protected $source = 'web.baituor.3910';
    protected $systemParam = [];

    protected $url = 'https://apiqae.qunar.com/api/router';

    public function __construct()
    {
        $this->systemParam['key'] = $this->key;
        $this->systemParam['token'] = $this->token;
        $this->systemParam['createTime'] = DateService::mtime();
    }

    /**
     * 航班搜索接口
     * @param $departCity
     * @param $arrivedCity
     * @param $date
     * @param $flightCode
     * @return mixed|void
     */
    public function flightPrice($departCity, $arrivedCity,$date,$flightCode)
    {
        $this->systemParam['tag'] = 'flight.national.ndc.searchprice';

        $param = [
            'depCityName'=>$departCity,
            'arrCityName'=>$arrivedCity,
            'code'=>$flightCode,
            'depDate'=>$date,
            'exTrack'=>'chailv',
            'useUnion'=>'false',
        ];

        /*$param = [
            'depCityName'=>'LHW',
            'arrCityName'=>'TNA',
            'code'=>'SC8710',
            'depDate'=>'2024-01-30',
            'exTrack'=>'chailv',
            'useUnion'=>'false',
        ];*/

        $this->systemParam['params'] = json_encode($param,JSON_UNESCAPED_UNICODE);
        $param = $this->makeSign($this->systemParam);

        $res = CurlService::post($this->url,$param,['content-type:application/x-www-form-urlencoded'],2);
        $res = json_decode($res,true);
        return $res;
    }

    /**
     * 生成签名
     * @param $params
     * @return void
     */
    protected function makeSign($params){

        ksort($params);
        $signStr = '';
        foreach ($params as $key=>$value) {

            $signStr .= $key.'='.$value;
        }


        $params['sign'] = md5($signStr);
        return $params;
    }

    /**
     * 前端获取价格
     * @param $departCityCode
     * @param $arrivedCityCode
     * @param $date
     * @param $flightCode
     * @param $cookie
     * @return mixed
     */
    public function flightPriceTwo($departCityCode, $arrivedCityCode,$date,$flightCode,$cookie)
    {

        $param = [
            'depCity'=>$departCityCode,
            'arrCity'=> $arrivedCityCode,
            'flightNum'=> $flightCode,
            'depDate'=> $date
        ];

        $url = 'http://fwwnsl.51taopiao.cn/api/flight/price/search';

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url.'?'.http_build_query($param),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',

            CURLOPT_HTTPHEADER => $this->header($departCityCode, $arrivedCityCode,$date,$flightCode,$cookie),
        ));

        $response = curl_exec($curl);

        curl_close($curl);

        $contentArray = json_decode($response,true);

        if (!empty($contentArray['ret']) && !empty($contentArray['data']) && !empty($contentArray['data']['vendors'])) {

            foreach ($contentArray['data']['vendors'] as $key => $value) {
                if ($value['productName'] == '蜗牛标品') {

                    return $value;
                }
            }
        }


        return false;

    }

    /**
     * 设置header
     * @param $cookie
     * @return string[]
     */
    private function header($departCityCode, $arrivedCityCode,$date,$flightCode,$cookie){

         $header = [
            'Accept:application/json, text/javascript, */*; q=0.01',
            'Accept-Language:zh-CN,zh;q=0.9',
            'Cache-Control:no-cache',
            'Connection:keep-alive',
            'Cookie:'.$cookie,
            'Host:fwwnsl.51taopiao.cn',
            'Pragma:no-cache',
            'Referer:http://fwwnsl.51taopiao.cn/uipage/search/index?'.urlencode('depCity='.Common::getCity($departCityCode).'('.$departCityCode.')&arrCity='.Common::getCity($arrivedCityCode).'('.$arrivedCityCode.')').'&depDate='.$date,
            'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
            'X-Requested-With:XMLHttpRequest',
        ];


        return $header;
    }
}